package com.educatioin.manage.web.pointreward.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.educatioin.commons.util.AjaxResult;
import com.educatioin.manage.web.pointreward.po.PointRewardOrderPo;
import com.educatioin.manage.web.pointreward.po.PointRewardProductPo;
import com.educatioin.manage.web.pointreward.repository.PointRewardProductRepository;
import com.educatioin.manage.web.pointreward.request.ListOrderReq;
import com.educatioin.manage.web.pointreward.request.ModifyOrderReq;
import com.educatioin.manage.web.pointreward.response.ListOrderRsp;
import com.educatioin.manage.web.pointreward.service.PointRewardOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by zhangyin on 2018/5/1.
 */
@Controller
@RequestMapping("/api/pointRewardOrder")
public class PointRewardOrderController {

    @Autowired
    private PointRewardOrderService pointRewardOrderService;
    @Autowired
    private PointRewardProductRepository pointRewardProductRepository;

    /**
     * 查看积分订单列表
     * @param req
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ListOrderRsp list(ListOrderReq req) {
        return pointRewardOrderService.list(req);
    }

    /**
     * 修改积分订单
     * @param req
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public AjaxResult modify(@RequestBody ModifyOrderReq req) {
        AjaxResult result = new AjaxResult<>();
        pointRewardOrderService.modify(req);
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("modify success");
        return result;
    }

    /**
     * 关闭订单
     * @param id
     * @return
     */
    @RequestMapping("/close")
    @ResponseBody
    public AjaxResult close(Long id) {

        return pointRewardOrderService.close(id);
    }

    @RequestMapping("/send")
    @ResponseBody
    public AjaxResult send(Long id) {
        return pointRewardOrderService.send(id);
    }

    @RequestMapping("/complete")
    @ResponseBody
    public AjaxResult complete(Long id) {
        return pointRewardOrderService.complete(id);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public AjaxResult findById(Long id) {
        AjaxResult<PointRewardOrderPo> result = new AjaxResult<>();
        result.setData(pointRewardOrderService.findById(id));
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }

    /**
     * 根据积分订单编号获取积分订单物品
     * @param id
     * @return
     */
    @RequestMapping("/findDetailById")
    @ResponseBody
    public AjaxResult findDetailById(Long id) {
        AjaxResult<String> result = new AjaxResult<>();
        PointRewardOrderPo pointRewardOrderPo = pointRewardOrderService.findById(id);
        StringBuilder sb = new StringBuilder();
        if(null!= pointRewardOrderPo){
            Map<String,Integer> map = JSON.parseObject(pointRewardOrderPo.getItems(),new TypeReference<Map<String,Integer>>(){});
            for (Map.Entry<String,Integer> entry:map.entrySet()){
                PointRewardProductPo pointRewardProductPo = pointRewardProductRepository.findByCode(entry.getKey()).get(0);
                sb.append(pointRewardProductPo.getName()).append(" * ").append(entry.getValue()).append("\n");
            }

        }
        result.setData(sb.toString());
        result.setCode(AjaxResult.SUCCESS_CODE);
        result.setMsg("success");
        return result;
    }

//    public static void main(String[] args) {
//        Map<String,Integer> map = new HashMap<>();
//        map.put("00001",10);
//        map.put("00002",5);
//        System.out.print(JSON.toJSONString(map));
//    }
}
